package logic

import (
	"context"
	"fmt"
	"weekly/model/mysql"
	"weekly/pkg"
	"weekly/weeklyrpc/internal/svc"
	"weekly/weeklyrpc/weeklyrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuthRentLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAuthRentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthRentLogic {
	return &AuthRentLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AuthRentLogic) AuthRent(in *weeklyrpc.AuthRentRequest) (*weeklyrpc.AuthRentResponse, error) {
	// todo: add your logic here and delete this line
	auth := mysql.UserRealNames{
		UsersId:  int(in.UsersId),
		IdCard:   in.CardNo,
		RealName: in.RealName,
	}
	var err error

	if err = auth.RealList(); err != nil {
		logx.Error("实名认证失败")
		return nil, fmt.Errorf("实名认证失败")
	}
	if auth.ID > 0 {
		logx.Error("实名用户不存在")
		return nil, fmt.Errorf("实名用户不存在")
	}
	if !pkg.AuthRend(in.RealName, in.CardNo) {
		logx.Errorf("第三方方法调用失败")
		return nil, fmt.Errorf("第三方方法调用失败")
	}
	if err = auth.RealAdd(); err != nil {
		logx.Errorf("用户创建失败")
		return nil, fmt.Errorf("用户创建失败")
	}
	return &weeklyrpc.AuthRentResponse{
		Success: true,
		Message: "实名认证成功",
	}, nil
}
